package com.gufe.service.user.impl.mp.quiz;

import com.gufe.dto.quiz.AnswerOptionDTO;
import com.gufe.dto.quiz.QuestionDTO;
import com.gufe.dto.quiz.SubmitPointsDTO;
import com.gufe.entity.quiz.AnswerOption;
import com.gufe.entity.quiz.Question;
import com.gufe.entity.quiz.UserPointRecord; // 更改导入的实体
import com.gufe.mapper.user.mp.quiz.AnswerOptionMapper;
import com.gufe.mapper.user.mp.quiz.QuestionMapper;
import com.gufe.mapper.user.mp.quiz.UserPointRecordMapper; // 更改导入的 Mapper
import com.gufe.service.user.mp.quiz.QuizService;
import com.gufe.service.user.mp.quiz.WxUserService;
import com.gufe.vo.quiz.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class QuizServiceImpl implements QuizService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private AnswerOptionMapper answerOptionMapper;


    @Autowired
    private WxUserService wxUserService; // 注入 UserService
    @Autowired
    private UserPointRecordMapper userPointRecordMapper; // 更改注入的 Mapper

    @Override
    public List<QuestionDTO> getQuestionsByCompetitionId(Long competitionId) {
        List<Question> questions = questionMapper.findByCompetitionId(competitionId);
        List<QuestionDTO> questionDTOs = new ArrayList<>();
        for (Question question : questions) {
            QuestionDTO questionDTO = new QuestionDTO();
            questionDTO.setId(question.getId());
            questionDTO.setQuestionText(question.getQuestionText());
            List<AnswerOption> answerOptions = answerOptionMapper.findByQuestionId(question.getId());
            List<AnswerOptionDTO> answerOptionDTOs = new ArrayList<>();
            for (AnswerOption option : answerOptions) {
                AnswerOptionDTO optionDTO = new AnswerOptionDTO();
                optionDTO.setId(option.getId());
                optionDTO.setOptionText(option.getOptionText());
                optionDTO.setIscorrect(option.getIsCorrect());
                answerOptionDTOs.add(optionDTO);
            }
            questionDTO.setOptions(answerOptionDTOs);
            questionDTOs.add(questionDTO);
        }
        return questionDTOs;
    }

    @Override
    @Transactional
    public ResultVO submitPoints(SubmitPointsDTO submitPointsDTO) {
        System.out.println("Received SubmitPointsDTO: " + submitPointsDTO); // 添加这行日志

        UserPointRecord userPointRecord = new UserPointRecord();
        userPointRecord.setWxusersId(submitPointsDTO.getWxusersId());
        userPointRecord.setCompetitionId(submitPointsDTO.getCompetitionId());
        userPointRecord.setTotalPoint(submitPointsDTO.getTotalPoint()); // 修改了这一行
        userPointRecord.setSubmitTime(LocalDateTime.now());

        userPointRecordMapper.insert(userPointRecord);

        wxUserService.updatePoints(submitPointsDTO.getWxusersId(), submitPointsDTO.getTotalPoint().intValue());

        ResultVO resultVO = new ResultVO();
        resultVO.setScore(submitPointsDTO.getTotalPoint().intValue());
        resultVO.setPoints(submitPointsDTO.getTotalPoint().intValue());
        return resultVO;
    }
}